package models

import "gorm.io/gorm"

// Institution 机构
type Institution struct {
	InstitutionID int `gorm:"primary_key" json:"institution_id"` // 机构ID

	InstitutionName string `json:"institution_name"` // 机构名称
	Contact         string `json:"contact"`          // 机构联系人
}

// TableName 表名称
func (*Institution) TableName() string {
	return "institution"
}

// GetInstitution 查询多条数据
func GetInstitution(fields []string, maps map[string]interface{}) ([]*Institution, error) {
	var address []*Institution
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&address).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return address, nil
}

func GetInstitutionPage(fields []string, maps map[string]interface{}, pageNum int, pageSize int, order string) ([]*Institution, error) {
	var Institution []*Institution
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Offset(pageNum).Limit(pageSize).Order(order).Find(&Institution).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return Institution, nil
}

// GetInstitutionInfo 查询多条数据
func GetInstitutionInfo(fields []string, maps map[string]interface{}) (*Institution, error) {
	var Institution Institution
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&Institution).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return &Institution, nil
}

// AddInstitution 插入数据
func AddInstitution(data map[string]interface{}) (int, error) {
	address := Institution{
		InstitutionName: data["institution_name"].(string),
		Contact:         data["contact"].(string),
	}
	result := db.Select("contact", "institution_name").Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.InstitutionID, nil
}

// AddInstitution 插入数据
func AddInstitutionDB(data map[string]interface{}, db *gorm.DB) (int, error) {
	address := Institution{
		InstitutionName: data["institution_name"].(string),
		Contact:         data["contact"].(string),
	}
	result := db.Select("contact", "institution_name").Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.InstitutionID, nil
}

// EditInstitution modify a single Institution
func EditInstitution(where map[string]interface{}, data interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Institution{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

func GetInstitutionTotal(where map[string]interface{}) (int64, error) {
	var count int64
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return 0, errs
	}
	if err := db.Model(&Institution{}).Where(cond, vals...).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func CleanInstitutionDB(where map[string]interface{}, db *gorm.DB) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Unscoped().Where(cond, vals...).Delete(&Institution{}).Error; err != nil {
		return err
	}

	return nil
}
